//
//  NBStatusPictureView.swift
//  NBSinaWeibo
//
//  Created by ac on 16/5/25.
//  Copyright © 2016年 ac. All rights reserved.
//

import UIKit

//图片之间的间距
private let pictureMargin: CGFloat = 3
//最大宽度
private let maxWidth = ScreenWidth - 2 * CellMargin
//单张图片的宽度
private let itemWidth = (maxWidth - 2 * pictureMargin) / 3


class NBStatusPictureView: UICollectionView {
    
    private lazy var testLbl: UILabel = UILabel(title: "", fontSize: 30, textColor: UIColor.redColor())
    
    private let pictureID = "pictureCell"
    
    var pictures: [NBPicture]?{
        didSet{
//            print(pictures?.count)
            testLbl.text = "\(pictures?.count ?? 0)"
            let pSize = calculatePictureViewSize()
            self.snp_updateConstraints(closure: { (make) in
                make.size.equalTo(pSize)
            })
            
            self.reloadData()
        }
    }
    
    private func calculatePictureViewSize() -> (CGSize){
        
        let imageCount = pictures?.count ?? 0
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        
        if imageCount == 0 {
            return CGSizeZero
        }
        
        if imageCount == 1 {
            let imageSize = CGSizeMake(100, 150)
            layout.itemSize = imageSize
            return imageSize
        }
        layout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        
        if imageCount == 4 {
            
            let w = itemWidth * 2 + pictureMargin
            return CGSizeMake(w, w)
        }
        
        let row = CGFloat((imageCount - 1) / 3) + 1
        let maxHeight = row * itemWidth + (row - 1) * pictureMargin
        return CGSizeMake(maxWidth, maxHeight)
    }
    

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = pictureMargin
        layout.minimumInteritemSpacing = pictureMargin
        super.init(frame: frame, collectionViewLayout: layout)
        
        self.dataSource = self
        self.backgroundColor = UIColor.whiteColor()
        self.scrollsToTop = false
        
        registerClass(NBStatusPictureCell.self, forCellWithReuseIdentifier: pictureID)
        setupUI()
    }
    
    private func setupUI(){
        addSubview(testLbl)
        
        testLbl.snp_makeConstraints { (make) in
            make.center.equalTo(self.snp_center)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    

}

extension NBStatusPictureView: UICollectionViewDataSource{
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return (pictures?.count) ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(pictureID, forIndexPath: indexPath) as! NBStatusPictureCell
        cell.backgroundColor = UIColor.purpleColor()
        cell.picture = pictures![indexPath.item]
        return cell
        
    }
}


class NBStatusPictureCell: UICollectionViewCell {
    
    private lazy var imgView: UIImageView = {
        let imgView = UIImageView()
        imgView.contentMode = .ScaleAspectFill
        imgView.clipsToBounds = true
        return imgView
    }()
    
    var picture:NBPicture? {
        didSet{
            let url = NSURL(string: picture?.thumbnail_pic ?? "")
            
            imgView.sd_setImageWithURL(url)
        }
    }
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置界面
    private func setupUI() {
        contentView.addSubview(imgView)
        //设置约束
        imgView.snp_makeConstraints { (make) in
            make.edges.equalTo(contentView.snp_edges)
        }
    }

}
